﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Diagnostics;
using Shapes = System.Windows.Shapes;

namespace GemBlaster.UI
{
    public class GameInstructions : Control
    {
        public GameInstructions()
        {
            Stream s = this.GetType().Assembly.GetManifestResourceStream("GemBlaster.UI.GameInstructions.xaml");
            FrameworkElement Root = this.InitializeFromXaml(new StreamReader(s).ReadToEnd());

            Canvas ReturnLink = Root.FindName("ReturnLink") as Canvas;

            ReturnLink.MouseLeftButtonDown += delegate(object sender, MouseEventArgs e)
            {
                if (ReturnLinkClick != null)
                {
                    ReturnLinkClick(this, EventArgs.Empty);
                }
            };

            Canvas NavLeft = Root.FindName("NavLeft") as Canvas;
            Storyboard NavLeftHover = Root.FindName("NavLeftHover") as Storyboard;

            NavLeft.MouseEnter += delegate(object sender, MouseEventArgs e)
            {
                NavLeftHover.Begin();
            };

            NavLeft.MouseLeave += delegate(object sender, EventArgs e)
            {
                NavLeftHover.Stop();
            };

            NavLeft.MouseLeftButtonDown += delegate(object sender, MouseEventArgs e)
            {
                MovePage(-1);
            };

            Canvas NavRight = Root.FindName("NavRight") as Canvas;
            Storyboard NavRightHover = Root.FindName("NavRightHover") as Storyboard;

            NavRight.MouseEnter += delegate(object sender, MouseEventArgs e)
            {
                NavRightHover.Begin();
            };

            NavRight.MouseLeave += delegate(object sender, EventArgs e)
            {
                NavRightHover.Stop();
            };

            NavRight.MouseLeftButtonDown += delegate(object sender, MouseEventArgs e)
            {
                MovePage(1);
            };

            _PageScroller = Root.FindName("PageScroller") as Storyboard;
        }

        public event EventHandler ReturnLinkClick;

        double _PageSize = 520;
        int _PageIndex = 0;
        Storyboard _PageScroller;

        void MovePage(int offset)
        {
            int newPage = _PageIndex + offset;
            if ((newPage >= 0) && (newPage < 4))
            {
                // animate position to new offset
                DoubleAnimationUsingKeyFrames Anim = _PageScroller.Children[0] as DoubleAnimationUsingKeyFrames;
                Anim.KeyFrames[0].Value = - _PageIndex * _PageSize;
                Anim.KeyFrames[1].Value = - newPage * _PageSize;
                _PageScroller.Begin();
                // update current index
                _PageIndex = newPage;
            }
        }

    }
}